package ControlLayer;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedList;
import DBLayer.DBConnection;
import DBLayer.DBSale;
import DBLayer.IFDBSale;
import ModelLayer.ActivityBooking;
import ModelLayer.Employee;
import ModelLayer.Guest;
import ModelLayer.KeyWord;
import ModelLayer.RoomBooking;
import ModelLayer.Sale;

public class SaleCtr {

	private RoomBookingCtr rbCtr;

	public SaleCtr() {
		rbCtr = new RoomBookingCtr();
	}

	public int updateSalePrice(int id, double price) {
		IFDBSale dbSale = new DBSale();
		return dbSale.updateSalePrice(id, price);
	}

	public Sale getSale(int saleId) {
		IFDBSale dbSale = new DBSale();
		return dbSale.getSale(saleId, true);
	}

	public int insertSale2(int guestId, int employeeId, double totalPrice,
			String status, String createDate, String payDate, String payMethod)
			throws ParseException {
		int saleId = 0;
		Sale sale = new Sale();
		sale.setGuest(new Guest(guestId));
		sale.setEmployee(new Employee(employeeId));
		sale.setCreateDate(new SimpleDateFormat("dd/MM/yyyy").parse(createDate));
		sale.setPayStatus(KeyWord.valueOf(status));
		sale.setTotalPrice(totalPrice);

		if (!payDate.isEmpty())
			sale.setPayDate(new SimpleDateFormat("dd/MM/yyyy").parse(payDate));
		if (!payMethod.isEmpty())
			sale.setPayMethod(KeyWord.valueOf(payMethod));
		try {
			DBConnection.startTransaction();
			DBSale dbSale = new DBSale();
			saleId = dbSale.insertSale(sale);
			DBConnection.commitTransaction();
		} catch (Exception e) {
			DBConnection.rollbackTransaction();
		}
		return saleId;
	}

	public boolean createSale(Sale sale) throws RoomBookingException {
		boolean success = true;
		RoomBooking roomBooking = sale.getRoomBooking();

		// check whether roombooking is available, then modify the booking year
		if (!rbCtr.isRoomBookingAvailable(roomBooking.getStartDate(),
				roomBooking.getEndDate(), roomBooking)) {
			success = false;
			return success;
		}
		// insert sale
		int saleId = insertSale(sale);
		// insert roombooking
		if (roomBooking != null)
			rbCtr.createRoomBooking(saleId, roomBooking.getCreateDate(),
					roomBooking.getStartDate(), roomBooking.getEndDate(),
					roomBooking.getDeposit(), roomBooking.getRent(),
					roomBooking.getPayStatus().toString(), roomBooking);
		return success;
	}

	public int insertSale(Sale sale) {
		int saleId = 0;
		DBSale dbSale = new DBSale();
		saleId = dbSale.insertSale(sale);
		return saleId;
	}

	public boolean createSale2(int guestId, int employeeId, double totalPrice,
			String status, String createDate, String payDate, String payMethod,
			RoomBooking roomBooking, ArrayList<ActivityBooking> activityBookings)
			throws ParseException, RoomBookingException {
		boolean success = true;
		// check whether roombooking is available, then modify the booking year
		if (!rbCtr.isRoomBookingAvailable(roomBooking.getStartDate(),
				roomBooking.getEndDate(), roomBooking)) {
			success = false;
			return success;
		}
		// insert sale
		int saleId = insertSale2(guestId, employeeId, totalPrice, status,
				createDate, payDate, payMethod);

		// insert roombooking
		if (roomBooking != null)
			rbCtr.createRoomBooking(saleId, roomBooking.getCreateDate(),
					roomBooking.getStartDate(), roomBooking.getEndDate(),
					roomBooking.getDeposit(), roomBooking.getRent(),
					roomBooking.getPayStatus().toString(), roomBooking);

		// insert activitybooking
		if (!activityBookings.isEmpty()) {
			// TODO inser tactivitybooking
		}
		return success;
	}

	public boolean updateSale(Sale oldsale, Sale newsale) throws SaleException,
			RoomBookingException {
		// TODO
		boolean success = true;
		// update roombooking
		rbCtr.updateDBRoomBooking(oldsale.getRoomBooking(),
				newsale.getRoomBooking());
		// update sale
		updateSaleInfo2(newsale);
		return success;
	}

	public void updateSaleInfo2(Sale sale) throws SaleException {
		IFDBSale dbSale = new DBSale();
		if (dbSale.updateSale(sale) == -1) {
			throw new SaleException("Sale update is failed");
		}
	}

	public void updateSaleInfo(int saleId, int guestId, int employeeId,
			double totalPrice, String status, String createDate,
			String payDate, String payMethod,
			ArrayList<RoomBooking> roomBookings,
			ArrayList<ActivityBooking> activityBookings) throws ParseException {
		Sale sale = new Sale();
		sale.setId(saleId);
		sale.setGuest(new Guest(guestId));
		sale.setEmployee(new Employee(employeeId));
		sale.setCreateDate(new SimpleDateFormat("dd/MM/yyyy").parse(createDate));
		sale.setPayDate(new SimpleDateFormat("dd/MM/yyyy").parse(payDate));
		sale.setPayStatus(KeyWord.valueOf(status));
		sale.setPayMethod(KeyWord.valueOf(payMethod));
		sale.setTotalPrice(totalPrice);
		// update sale
		IFDBSale dbSale = new DBSale();
		dbSale.updateSale(sale);
		// update bookings

	}

	public int deleteSale(int saleId) {
		// delete room booking
		RoomBooking rb = rbCtr.getRoomBooking(saleId);
		if (rb != null) {
			rbCtr.deleteRoomBooking(saleId);
		}

		// TODO delete activity booking

		// delete sale
		IFDBSale dbSale = new DBSale();
		return (dbSale.deleteSale(saleId));
	}

	public LinkedList<Sale> getAllSale() {
		IFDBSale dbSale = new DBSale();
		LinkedList<Sale> sales = new LinkedList<Sale>();
		sales = dbSale.getAllSale(false);
		return sales;
	}

}
